package threadUtils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @program: javase
 * @description: CyclicBarrier 加法计数器
 * @author: lld
 * @create: 2021-04-18 19:48
 **/
public class CyclicBarrierTest {
    public static void main(String[] args) {
//        7表示 屏障跳闸前必须调用 await()的线程数
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("7龙珠集齐，召唤神龙");
        });
        for (int i = 1; i <= 7; i++) {
            final int num = i;
            new Thread(() -> {
                System.out.println(num + "龙珠有了");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }

    }
}
